package com.source.tiershop.server;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.xml.bind.JAXBElement;
import javax.xml.datatype.XMLGregorianCalendar;

import webservices.framework.affilinet.types.productservice.ArrayOfShop;
import webservices.framework.affilinet.types.productservice.Shop;
import webservices.framework.affilinet.types.productservice.ShopInformation;
import webservices.framework.affilinet.types.productservice.ShopList;

import com.source.tiershop.client.GreetingService;
import com.source.tiershop.shared.CShop;
import com.source.tiershop.shared.FieldVerifier;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class GreetingServiceImpl extends RemoteServiceServlet implements
		GreetingService {

	private static webservices.framework.affilinet.types.productservice.ObjectFactory typesProductObjectFactory =
		new webservices.framework.affilinet.types.productservice.ObjectFactory();

	
	public String greetServer(String input) throws IllegalArgumentException {
		// Verify that the input is valid. 
		if (!FieldVerifier.isValidName(input)) {
			// If the input is not valid, throw an IllegalArgumentException back to
			// the client.
			throw new IllegalArgumentException(
					"Name must be at least 4 characters long");
		}

		String credentialToken = null;
		credentialToken = AuthenticationWrapper.logon();
		
		XMLGregorianCalendar tokenDate = AuthenticationWrapper.getExpiration(credentialToken);
	
		return "Hello4, " + "----- " + credentialToken + " --- expiration --- " + tokenDate.toString();
	}
	
	public List<String> greetServer2(String input) throws IllegalArgumentException {
		// Verify that the input is valid. 
//		if (!FieldVerifier.isValidName(input)) {
//			// If the input is not valid, throw an IllegalArgumentException back to
//			// the client.
//			throw new IllegalArgumentException(
//					"Name must be at least 4 characters long");
//		}

		String credentialToken = null;
		credentialToken = AuthenticationWrapper.logon();
		
//		XMLGregorianCalendar tokenDate = AuthenticationWrapper.getExpiration(credentialToken);
	
		ShopList shopList = ProductWrapper.getShopList();
		
//		List<String> listOfShops = Arrays.asList("Sunday", "Monday",
//			      "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
		
	
 			JAXBElement<ArrayOfShop> shops = shopList.getShops();
 			
 			List<Shop> shopsValue = shops.getValue().getShop();
 			
 			List<String> listOfShops = new ArrayList<String>();
 				
 			String shopName = null;
// 			for (Shop shop : shopsValue) {
// 				shopName = shop.getTitle().getValue();
// 				if (listOfShops.isEmpty()) {
// 					listOfShops = Arrays.asList(shopName);
//				}
// 				else
// 				{
// 					listOfShops. add(shopName);
// 				}
//				
//			}
 			
 		    for(int i = 0, size = shopsValue.size(); i<size; i++){
 		    	shopName = shopsValue.get(i).getTitle().getValue();
 				listOfShops.add(shopName);
		     }
 		    
		return listOfShops;
	}

	
	public List<CShop> greetServer3(String input) throws IllegalArgumentException {
		String credentialToken = null;
		credentialToken = AuthenticationWrapper.logon();
		
//		XMLGregorianCalendar tokenDate = AuthenticationWrapper.getExpiration(credentialToken);
	
		ShopList shopList = ProductWrapper.getShopList();
		
	
 			JAXBElement<ArrayOfShop> shops = shopList.getShops();
 			
 			List<Shop> shopsList = shops.getValue().getShop();
 			
// 			List<String> listOfShops = new ArrayList<String>();
// 				
// 			String shopName = null;
//
// 			
 			ArrayList<CShop> cShopList = new ArrayList<CShop>();
 			CShop 			 cShop	   = new CShop();
 			
			
 		    for(int i = 0, size = shopsList.size(); i<size; i++){
 		    	Shop shop = shopsList.get(i);
 		    	cShop = new CShop(shop.getShopId(),
 		    					  shop.getProgramId(), 
 		    					  shop.getTitle().getValue(),
 		    					  shop.getLastUpdate().getValue().toGregorianCalendar().getTime(),
 		    					  shop.getLogoUrl().getValue(),
 		    					  shop.getProducts());
 		    	
 				cShopList.add(cShop);
		     }
 		    
		return cShopList;
	}
	
	
	
}
